;(function () {
    $.fn.extend({
        /**
         * 计算总价的函数
         */
        calculateSum() {
            const self = this;

            let userObj = JSON.parse(localStorage.getItem('userInfo') || '{}');
            if (!userObj.u_id) {//如果没登录就切换到登录
                layer.msg('请您先登录');
                setTimeout(function () {
                    location.href = 'login.html';
                }, 1000);
                return;
            }

            //更新单个商品数据
            function updateItem(index = 0) {
                let controlLi = $('.goods-car>li').eq(index);
                let sumCount = Math.abs(parseInt(controlLi.find('.goodsNum').val()));
                $.ajax({
                    url: "/cart/modify",
                    type: 'post',
                    data: {
                        cId: $(controlLi).data("cid"),
                        cNum: sumCount
                    }
                }).then(function (res) {
                    if (res.status === 200) {
                        RA.render();
                        RA.renderAside();
                    }
                })
            }

            RA.render();
            RA.renderAside();//初始化
            //单个多选框事件
            $(self).on('click', '.goods .selectItem', function () {
                let mine = $(this);
                let selected = $(this).prop('checked');
                $.ajax({
                    url: './cart/selected',
                    type: 'post',
                    data: {
                        cDesc: selected ? '1' : '0',
                        cId: $(mine).parents('li').data('cid')
                    }
                }).then(function (res) {
                    if (res.status === 200) {
                        RA.render();
                        RA.renderAside();
                    }
                })
            });
            //全选多选框事件
            $(self).find('.selectAll').on('click', function () {
                let selected = $(this).prop('checked');
                $.ajax({
                    url: './cart/selectAll',
                    type: 'post',
                    data: {
                        cDesc: selected ? '1' : '0',
                        uId: userObj.u_id
                    }
                }).then(function (res) {
                    if (res.status === 200) {
                        RA.render();
                        RA.renderAside();
                    }
                });
            });

            //加减事件
            $(self).on('click', '.u-setCount .add', function () {
                let sumCount = Math.abs(parseInt($(this).prev().val()));
                $(this).prev().val(++sumCount);
                updateItem($(this).parents('li').index());
            })

            $(self).on('click', '.u-setCount .reduce', function () {
                let sumCount = Math.abs(parseInt($(this).next().val()));
                if (sumCount > 1) {
                    $(this).next().val(--sumCount);
                    updateItem($(this).parents('li').index());
                }
            })

            //输入事件
            $(self).on('change', '.goodsNum', function () {
                let sumCount = Math.abs(parseInt($(this).val()));
                if (sumCount === 0) {
                    sumCount = 1;
                }
                $(this).val(sumCount);
                updateItem($(this).parents('li').index());
            })

            //删除事件
            $(self).on('click', '.delete', function () {
                let cId = $(this).parents('li').data('cid');
                layer.confirm('您确定要舍弃这个宝贝吗?', {
                    btn: ['残忍舍弃', '还是算了']
                }, function (index) {
                    $.ajax({
                        url: './cart/delete',
                        type: 'post',
                        data: {cId}
                    }).done(function (res) {
                        layer.msg(res.msg);
                        RA.render();
                        RA.renderAside();
                    })
                    layer.close(index);
                }, function () {

                })
            });

            //删除选中事件
            $(self).find('.deleteSelect').on('click', function () {
                layer.confirm('您确定要舍弃选中的这些宝贝吗?', {
                    btn: ['残忍舍弃', '还是算了']
                }, function (index) {
                    $.ajax({
                        url: './cart/deleteSelected',
                        type: 'post',
                        data: {uId: userObj.u_id}
                    }).done(function (res) {
                        layer.msg(res.msg);
                        RA.render();
                        RA.renderAside();
                    })
                    layer.close(index);
                }, function () {

                })
            })
        }
    })
})()

$(function () {
    $('.myCarBox').calculateSum();
})